﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class RepairListCtrl : BaseListControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
        }

        public View_RepairJobProblems JobProblems
        {
            get
            {
                if (Session["View_RepairJobProblems"] == null)
                    Session["View_RepairJobProblems"] = View_RepairJobProblems.GetCurrentJobProblems();
                return (View_RepairJobProblems)Session["View_RepairJobProblems"];
            }
            set
            {
                Session["View_RepairJobProblems"] = value;
            }
        }

        public String RepairedOrUnrepaired(Object statusID, Object invoiceID)
        {
            if (statusID != null && statusID != DBNull.Value && (int)statusID == RepairStatus.Returned)
            {
                if (invoiceID == null || invoiceID == DBNull.Value)
                    return "Unrepaired";
                else
                    return "Repaired";
            }
            else
                return "";
        }

        public bool IsJobReturned(Object statusID)
        {
            if (statusID != null && statusID != DBNull.Value && (int)statusID == RepairStatus.Returned)
               return true;
            else
                return false;
        }


        View_RepairStatusHistories jobStatusHist;
        public View_RepairStatusHistories JobStatusHist
        {
            get
            {
                if (jobStatusHist == null)
                    jobStatusHist = View_RepairStatusHistories.GetCurrentJobHists();
                return jobStatusHist;
            }

            set
            {
                jobStatusHist  = value;
            }
        }

        public View_RepairJobs Jobs 
        {
            get
            {
                return (View_RepairJobs)SourceObject;
            }
            set
            {
                SourceObject = value;
            }
        }

        

        public string ClientOnEditProblemPrototype
        {
            get { return ViewState[ClientID + "_ClientOnEditProblem"] != null ? ViewState[ClientID + "_ClientOnEditProblem"].ToString() : "doNothing"; }
            set { ViewState[ClientID + "_ClientOnEditProblem"] = value; }
        }

        public string ClientOnCallCustomerPrototype
        {
            get { return ViewState[ClientID + "_ClientOnCallCustomerPrototype"] != null ? ViewState[ClientID + "_ClientOnCallCustomerPrototype"].ToString() : "doNothing"; }
            set { ViewState[ClientID + "_ClientOnCallCustomerPrototype"] = value; }
        }

        public string ClientOnUpdateStatusPrototype
        {
            get { return ViewState[ClientID + "_ClientOnUpdateStatusPrototpye"] != null ? ViewState[ClientID + "_ClientOnUpdateStatusPrototpye"].ToString() : "doNothing"; }
            set { ViewState[ClientID + "_ClientOnUpdateStatusPrototpye"] = value; }
        }

        public string ClientOnChangeRepairBoxPrototype
        {
            get { return ViewState[ClientID + "_ClientOnChangeRepairBoxPrototype"] != null ? ViewState[ClientID + "_ClientOnChangeRepairBoxPrototype"].ToString() : "doNothing"; }
            set { ViewState[ClientID + "_ClientOnChangeRepairBoxPrototype"] = value; }
        }

        public bool AllowUpdating
        {
            get { return ViewState[ClientID + "_AllowUpdate"] != null ? (bool)ViewState[ClientID + "_AllowUpdate"] : true; }
            set { ViewState[ClientID + "_AllowUpdate"] = value; }
        }

        

        public String GetProblems(Object jobID)
        {
            if (JobProblems == null)
                return "";

            String res = "";
            String oldFilter = JobProblems.Filter;
            JobProblems.Filter = View_RepairJobProblems.ColumnNames.RepairJobID + "=" + jobID.ToString();

            if (!AllowUpdating || Branches.CurrentBranchID != SessionUser.BranchID)
                ClientOnEditProblemPrototype = "doNothing";

            if (JobProblems.RowCount > 0)
            {
                JobProblems.Rewind();
                for (int i = 0; i < JobProblems.RowCount; i++)
                {
                    string str = "";
                    if(JobProblems.s_IsRedo != "" && JobProblems.IsRedo)
                        str = String.Format("<div class='redo' onclick='{3}({2});'>{0}&nbsp;&nbsp;{1:C}</div>", JobProblems.Description, JobProblems.Price, JobProblems.ID, ClientOnEditProblemPrototype);
                    else
                        str = String.Format("<div onclick='{3}({2});'>{0}&nbsp;&nbsp;{1:C}</div>", JobProblems.Description, JobProblems.Price, JobProblems.ID, ClientOnEditProblemPrototype);
                    res += str;

                    JobProblems.MoveNext();
                }
            }

            JobProblems.Filter = oldFilter;

            return res;
        }

        public string GetRepairBox(Object boxName, Object boxID, Object jobID)
        {
            string str = "";

            if (!AllowUpdating)
                ClientOnChangeRepairBoxPrototype = "doNothing";

            if (boxName == null || boxName == DBNull.Value || boxName.ToString() == "")
                boxName = "n/a";
            if (boxID == null || boxID == DBNull.Value)
                boxID = 0;

            str = string.Format("<span class='labelBarCode' onclick='{0}({2}, {3})'>{1}</span>", ClientOnChangeRepairBoxPrototype, boxName, boxID, jobID);

            return str;
        }

        public String GetRepairStatus(Object jobID)
        {
            if (JobProblems == null)
                return "";

            String res = "";
            String oldFilter = JobStatusHist.Filter;
            JobStatusHist.Filter = View_RepairStatusHistories.ColumnNames.RepairJobID + "=" + jobID.ToString();
            JobStatusHist.Sort = View_RepairStatusHistories.ColumnNames.UpdatedDate + " desc";
            if (JobStatusHist.RowCount > 0)
            {
                JobStatusHist.Rewind();
                for (int i = 0; i < JobStatusHist.RowCount; i++)
                {
                    string str = "<table><tr>";
                    str += string.Format("<td style='width:80px; text-align:left; vertical-align:top'>{0}</td>", JobStatusHist.UpdatedDate.ToString(SysVars.DateFormat));
                    str += string.Format("<td style='width:250px; text-align:left; vertical-align:top'>{0}</td>", JobStatusHist.StatusName);
                    str += string.Format("<td style='width:100px; text-align:left; vertical-align:top'>{0}</td>", JobStatusHist.s_UpdatedFullName != "" ? JobStatusHist.s_UpdatedFullName : JobStatusHist.s_UpdatedByCustomerName);
                    str += "</tr></table>";

                    res += str;

                    JobStatusHist.MoveNext();
                }
            }

            JobStatusHist.Filter = oldFilter;

            return res == "" ? "n.a" : res;
        }

        public int[] GetCheckedItemIDs()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);

            String[] ids = checkedItemID.Split(new char[] { ',' });
            int[] items = new int[ids.Length];
            for (int i = 0; i < ids.Length; i++)
            {
                items[i] = int.Parse(ids[i]);
            }

            return items;
        }

        public string GetCheckedStrItemIDs()
        {
            String checkedItemID = "";
            for (int i = 0; i < grid.Items.Count; i++)
            {
                DataGridItem item = grid.Items[i];
                Control ctrl = item.FindControl("cbSelect");
                if (ctrl != null && ctrl is CheckBox && ((CheckBox)ctrl).Checked && ((CheckBox)ctrl).Attributes["ItemID"] != null)
                    checkedItemID += ((CheckBox)ctrl).Attributes["ItemID"].ToString() + ",";
            }
            if (checkedItemID != "")
                checkedItemID = checkedItemID.Remove(checkedItemID.Length - 1);

            return checkedItemID;
        }

        protected void btnChangeLoanPhone_OnClick(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}